﻿using EFCoreLib01;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Diagnostics;

namespace AspNetCorePrj.Controllers
{

    public class EFCoreLibController : Controller
    {
        private readonly ILogger<EFCoreLibController> _logger;
   

        private readonly IServiceProvider _serviceProvider;
        private readonly IUnitOfWorkManager _unitOfWorkManager;

        public EFCoreLibController(ILogger<EFCoreLibController> logger,IServiceProvider serviceProvider)
        {
            _logger = logger;

            _serviceProvider = serviceProvider;

            _unitOfWorkManager = _serviceProvider.GetRequiredService<IUnitOfWorkManager>();
        }

        public IActionResult Index()
        {
            using (var uowHandle = _unitOfWorkManager.Begin<PersionDbContext>())
            {
                var db = uowHandle.GetActiveUnitOfWork();
                db.Database.EnsureCreated();
                //清理
                for (var i = 0; i < 100; i++)
                {
                    db.Blogs.Add(new Blog()
                    {
                        Url = "www.baidu.com",
                        Display = "哈哈哈",
                        IsEnabled = true,
                        CreatedTime = DateTime.Now,
                        DeletedTime = DateTime.Now,
                        UserId = 1
                    });
                }
                db.SaveChanges();

                try
                {
                    uowHandle.SaveChange();
                   
                }
                catch (Exception ex)
                {
                }
                return View();
            }

        }

        public IActionResult Privacy()
        {
            return View();
        }

       
    }
}